
var models = require('../models');
var hash = require('../hash.js').hash;
var fs = require('fs');
var url=require('url');
var userModel = models.userModel;
var callUserModel = models.callUserModel;
var pdfUserModel = models.pdfUserModel;
var pdfModel = models.pdfModel;
Array.prototype.find = function(ele){
  var len = this.length;
  for(var i = len-1; i >= 0; i--){
    if(this[i] == ele){
      return i;
    }
  }
  return -1;
}
function getStringKey(s,key){
	var eleArray = s.split('&');
	var eleNum = eleArray.length;
	for(var i = 0; i < eleNum; i++){
		var parts = eleArray[i].split('=');
		if(parts[0] == key){
			return parts[1];
		}
	}
	return null;
}
var pieTime = function(){
  var curTime = new Date();
  var year = curTime.getFullYear();
  var month = curTime.getMonth()+1;
  if(month<10){
    month = '0'+month;
  }
  var date = curTime.getDate();
  if(date<10){
    date = '0'+date;
  }
  var hour = curTime.getHours();
  if(hour<10){
    hour = '0'+hour;
  }
  var minute = curTime.getMinutes();
  if(minute<10){
    minute = '0'+minute;
  }
  var second = curTime.getSeconds();
  if(second<10){
    second = '0'+second;
  }
  var result = year+'-'+month+'-'+date+' '+hour+':'+minute+':'+second;
  return result;
}
function randomChar(l){
	var x="123456789poiuytrewqasdfghjklmnbvcxzQWERTYUIPLKJHGFDSAZXCVBNM";
 	var tmp="";
 	for(var i=0;i< l;i++) {
 		tmp += x.charAt(Math.ceil(Math.random()*100000000)%x.length);
 	}
 	return tmp;
}
function Trim(str,is_global){
	var result; 
 	result = str.replace(/(^\s+)|(\s+$)/g,"");
 	if(is_global.toLowerCase()=="g"){
 		result = result.replace(/\s/g,"");
 	}
 	return result;
}
exports.uploadGPS = function(req,res){
	console.log('uploadGPS here');
	var longtitude = req.body.longtitude;
	var latitude = req.body.latitude;
	console.log('longtitude:'+longtitude+',latitude:'+latitude);
	res.send({
		status:'success',
		msg:'ok'
	});
}
exports.course = function(req,res){
	res.render('course');
}
exports.pdf = function(req,res){
	res.render('pdf');
}
exports.regPdf = function(req,res){
	var err = null;
	if (req.body.role == '') {
		err = '身份不能为空';
	}
	if (req.body.password == '') {
		err = '密码不能为空';
	}
	if (req.body.nickname == '') {
		err = '昵称不能为空';
	}
	if (req.body.id == '') {
		err = '账号不能为空';
	}
	if (err) {
		res.send({
			status:'error',
			message:err
		});
		return;
	}
	pdfUserModel.findOne({ id: req.body.id }, function (err, pdfUser) {
        if (pdfUser) {
            res.send({
				status:'error',
				message:'账号已经被注册'
			});
			return;
        }
        hash(req.body.password, function (err, salt, hash) {
            if (err) {
                res.send({
            		status:'error',
            		message:JSON.stringify(err)
            	});
            	return;
            }
            var newPdfUser = new pdfUserModel({
                id: req.body.id,
                nickname: req.body.nickname,
                mobile:req.body.mobile,
                role:req.body.role,
                email:req.body.email,
                hash: hash,
                salt: salt,
                head: null,
                token:randomChar(50)
            });
            newPdfUser.save(function (err) {
                if (err) {
                    res.send({
            			status:'error',
            			message:JSON.stringify(err)
            		});
            		return;
                }
                res.send({
            		status:'success',
            		user:newPdfUser
            	});
            });
        });
    });
}
exports.loginPdf = function(req,res){
	var err = null;
	if (!req.body.password) {
		err = '请输入密码';
	}
	if (!req.body.id) {
		err = '请输入账号';
	}
	if (err) {
		res.send({
			status:'error',
			message:err
		});
		return;
	}
	pdfUserModel.findOne({ id: req.body.id }, function (err, pdfUser) {
        if (err) {
        	res.send({
				status:'error',
				message:JSON.stringify(err)
			});
            return;
        }
        if (!pdfUser) {
        	res.send({
				status:'error',
				message:'用户不存在'
			});
            return;
        }
        hash(req.body.password, pdfUser.salt, function (err, hash) {
            if (err) {
                res.send({
					status:'error',
					message:JSON.stringify(err)
				});
            	return;
            }
            if (hash == pdfUser.hash) {
                res.send({
					status:'success',
					user:pdfUser
				});
            }
            else{
            	res.send({
					status:'error',
					message:'密码不正确'
				});
            }
        });
    });
}
exports.getUserInfo = function(req,res){
	var urlQuery = url.parse(req.url).query;
	var userId = getStringKey(urlQuery,'userId');
	if(!userId){
		res.send({
			status:'error',
			message:'userId不能为空'
		});
		return;
	}
	var token = getStringKey(urlQuery,'token');
	if(!token){
		res.send({
			status:'error',
			message:'token不合法'
		});
		return;
	}
	pdfUserModel.findOne({id:userId},function(err,user){
		if(err){
			res.send({
				status:'error',
				message:JSON.stringify(err)
			});
		}
		else{
			if(user){
				if(user.token == token){
					res.send({
						status:'success',
						info:user
					})
				}
				else{
					res.send({
						status:'error',
						message:'token不合法'
					})
				}
			}
			else{
				res.send({
					status:'error',
					message:'userId不合法，对应用户不存在'
				})
			}
		}
	})
}
exports.getTargetInfo = function(req,res){
	var urlQuery = url.parse(req.url).query;
	var userId = getStringKey(urlQuery,'userId');
	if(!userId){
		res.send({
			status:'error',
			message:'userId不能为空'
		});
		return;
	}
	var token = getStringKey(urlQuery,'token');
	if(!token){
		res.send({
			status:'error',
			message:'token不合法'
		});
		return;
	}
	var targetId = getStringKey(urlQuery,'targetId');
	if(!targetId){
		res.send({
			status:'error',
			message:'targetId不能为空'
		});
		return;
	}
	pdfUserModel.findOne({id:userId},function(err,user){
		if(err){
			res.send({
				status:'error',
				message:JSON.stringify(err)
			});
		}
		else{
			if(user){
				if(user.token == token){
					pdfUserModel.findOne({id:targetId},function(err,target){
						if(err){
							res.send({
								status:'error',
								message:JSON.stringify(err)
							});
						}
						else{
							if(target){
								var targetInfo = {
									nickname:target.nickname,
									role:target.role,
									head:target.head
								}
								res.send({
									status:'success',
									info:targetInfo
								});
							}
							else{
								res.send({
									status:'error',
									message:'targetId不合法,对应用户不存在'
								});
							}
						}
					})
				}
				else{
					res.send({
						status:'error',
						message:'token不合法'
					})
				}
			}
			else{
				res.send({
					status:'error',
					message:'userId不合法，对应用户不存在'
				})
			}
		}
	})
}
exports.upload = function(req,res){
	//别把这个函数删了，这是网络实习课上的作业
	if(!req.files||!req.files.file||!req.files.file.name){
		res.send({
			status:'error',
			message:'请上传头像'
		});
		return;
	}
	var tmpPath = req.files.file.path;
	var userId = req.body.account;
	var date = new Date();
	var now = ''+date.getHours()+date.getMinutes()+date.getSeconds();
	var newName = Trim(req.files.file.name,'g');
	var targetPath = 'public/img/'+userId+'/'+now+newName;
	var newSrc = '/img/'+userId+'/'+now+newName;
	fs.exists('public/img/'+userId,function(exists){
		if(!exists){
			fs.mkdir('public/img/'+userId,function(err){
				if(!err){
					fs.rename(tmpPath,targetPath,function(err){
						if(err){
							res.send({'status':'error','message':'move file error'});
						}
						else{
							userModel.findOne({account:userId},function(err,user){
								if(err){
									res.send({
										status:'error',
										message:JSON.stringify(err)
									});
								}
								else{
									if(user){
										user.avatar = newSrc;
										user.save(function(err){
											if(err){
												res.send({
													status:'error',
													message:JSON.stringify(err)
												});
											}
											else{
												res.send({
													status:'success',
													src:newSrc
												});
											}
										})
									}
									else{
										res.send({
											status:'error',
											message:'userId不合法，对应用户不存在'
										});
									}
								}
							})
						}
					})
				}
				else{
					res.send({'status':'error','message':'mkdir error'});
				}
			})
		}
		else{
			fs.rename(tmpPath,targetPath,function(err){
				if(err){
					res.send({'status':'error','message':'move file error'});
				}
				else{
					userModel.findOne({account:userId},function(err,user){
						if(err){
							res.send({'status':'error','message':'connect database error'});
						}
						else{
							if(user){
								user.avatar = newSrc;
								user.save(function(err){
									if(err){
										res.send({'status':'error','message':'save user error'});
									}
									else{
										res.send({'status':'success','src':newSrc});
									}
								})
							}
							else{
								res.send({'status':'error','message':'userId不合法，对应用户不存在	'});
							}
						}
					})
				}
			})
		}
	})
}
exports.updateHead = function(req,res){
	if(!req.files||!req.files.file||!req.files.file.name){
		res.send({
			status:'error',
			message:'请上传头像'
		});
		return;
	}
	var tmpPath = req.files.file.path;
	var userId = req.body.account;
	var token = req.body.token;
	var date = new Date();
	var now = ''+date.getHours()+date.getMinutes()+date.getSeconds();
	var newName = Trim(req.files.file.name,'g');
	var targetPath = 'public/img/'+userId+'/'+now+newName;
	var newSrc = '/img/'+userId+'/'+now+newName;
	fs.exists('public/img/'+userId,function(exists){
		if(!exists){
			fs.mkdir('public/img/'+userId,function(err){
				if(!err){
					fs.rename(tmpPath,targetPath,function(err){
						if(err){
							res.send({'status':'error','message':'move file error'});
						}
						else{
							pdfUserModel.findOne({id:userId},function(err,user){
								if(err){
									res.send({
										status:'error',
										message:JSON.stringify(err)
									});
								}
								else{
									if(user){
										if(user.token != token){
											res.send({
												status:'error',
												message:'token不合法'
											});
											return;
										}
										user.head = newSrc;
										user.save(function(err){
											if(err){
												res.send({
													status:'error',
													message:JSON.stringify(err)
												});
											}
											else{
												res.send({
													status:'success',
													head:newSrc
												});
											}
										})
									}
									else{
										res.send({
											status:'error',
											message:'userId不合法，对应用户不存在'
										});
									}
								}
							})
						}
					})
				}
				else{
					res.send({'status':'error','message':'mkdir error'});
				}
			})
		}
		else{
			fs.rename(tmpPath,targetPath,function(err){
				if(err){
					res.send({'status':'error','message':'move file error'});
				}
				else{
					pdfUserModel.findOne({id:userId},function(err,user){
						if(err){
							res.send({'status':'error','message':'connect database error'});
						}
						else{
							if(user){
								if(user.token != token){
									res.send({
										status:'error',
										message:'token不合法'
									});
									return;
								}
								user.head = newSrc;
								user.save(function(err){
									if(err){
										res.send({'status':'error','message':'save user error'});
									}
									else{
										res.send({'status':'success','head':newSrc});
									}
								})
							}
							else{
								res.send({'status':'error','message':'userId不合法，对应用户不存在	'});
							}
						}
					})
				}
			})
		}
	})
}
exports.updateNickname = function(req,res){
	var userId = req.body.account;
	if (!userId) {
		res.send({
			status:'error',
			message:'userId不能为空'
		});
		return;
	}
	var token = req.body.token;
	if (!token) {
		res.send({
			status:'error',
			message:'token不能为空'
		});
		return;
	}
	var nickname = req.body.nickname;
	if (!nickname) {
		res.send({
			status:'error',
			message:'nickname不能为空'
		});
		return;
	}
	pdfUserModel.findOne({id: userId}, function(err, user) {
        if (err) {
        	res.send({
				status:'error',
				message:JSON.stringify(err)
			});
        }
        else{
        	if (!user) {
        		res.send({
					status:'error',
					message:'userId不合法，对应用户不存在'
				});
        	}
        	else{
        		if(user.token != token){
        			res.send({
        				status:'error',
        				message:'token不合法'
        			});
        			return;
        		}
        		user.nickname = nickname;
        		user.save(function(err){
        			if(err){
        				res.send({
        					status:'error',
        					message:JSON.stringify(err)
        				});
        			}
        			else{
        				res.send({
        					status:'success'
        				});
        			}
        		})
        	}
        }
    });
}
exports.replyToPdf = function(req,res){
	var pdfUrl = req.body.pdfUrl;
	if(!pdfUrl){
		res.send({
			status:'error',
			message:'pdfUrl不能为空'
		});
		return;
	}
	var pageIndex = parseInt(req.body.pageIndex);
	if(pageIndex<0){
		res.send({
			status:'error',
			message:'页码不能小于0'
		});
		return;
	}
	var type = parseInt(req.body.type);
	if(type != 0&&type != 1){
		res.send({
			status:'error',
			message:'评论类别必须为提问或者笔记'
		});
		return;
	}
	var content = req.body.content;
	if(!content){
		res.send({
			status:'error',
			message:'评论内容不能为空'
		});
		return;
	}
	var title = req.body.title;
	if(!title){
		res.send({
			status:'error',
			message:'title不能为空'
		});
		return;
	}
	var userId = req.body.userId;
	if(!userId){
		res.send({
			status:'error',
			message:'userId不合法，对应用户不存在'
		});
		return;
	}
	var token = req.body.token;
	if(!token){
		res.send({
			status:'error',
			message:'token不能为空'
		});
		return;
	}
	pdfUserModel.findOne({'id':userId},function(err,user){
		if(err){
			res.send({
				status:'error',
				message:JSON.stringify(err)
			})
		}
		else{
			if(!user){
				res.send({
					status:'error',
					message:'userId不合法，对应用户不存在'
				})
			}
			else{
				if(user.token != token){
					res.send({
						status:'error',
						message:'token不合法'
					});
					return;
				}
				pdfModel.findOne({'url':pdfUrl},function(err,pdf){
					if(err){
						res.send({
							status:'error',
							message:JSON.stringify(err)
						})
					}
					else{
						if(!pdf){
							pdf = new pdfModel({
								'url':pdfUrl,
								'page':[]
							});
						}
						var allPage = pdf.page;
						var targetIndex = -1;
						var pageNum = allPage.length;
						for(var i = 0; i < pageNum; i++){
							if(allPage[i].index == pageIndex){
								targetIndex = i;
								break;
							}
						}
						if(targetIndex<0){
							targetIndex = allPage.push({
								index:pageIndex,
								reply:[]
							})-1;
						}
						if(req.files&&req.files.file&&req.files.file.name){
							var tmpPath = req.files.file.path;
							var date = new Date();
							var now = ''+date.getHours()+date.getMinutes()+date.getSeconds();
							var newName = Trim(req.files.file.name,'g');
							var targetPath = 'public/attachments/'+userId+'/'+now+newName;
							var newSrc = '/attachments/'+userId+'/'+now+newName;
							fs.exists('public/attachments/'+userId,function(exists){
								if(!exists){
									fs.mkdir('public/attachments/'+userId,function(err){
										if(!err){
											fs.rename(tmpPath,targetPath,function(err){
												if(err){
													res.send({'status':'error','message':'move file error'});
												}
												else{
													allPage[targetIndex].reply.push({
														userId:userId,
														type:type,
														title:title,
														content:content,
														time:pieTime(),
														_time:Math.floor(new Date().getTime()),
														subReply:[],
														file:{
															url:newSrc,
															name:req.files.file.name
														}
													});
													pdf.save(function(err){
														if(err){
															res.send({
																status:'error',
																message:JSON.stringify(err)
															})
														}
														else{
															res.send({
																status:'success'
															})
														}
													});
												}
											})
										}
										else{
											res.send({'status':'error','message':'mkdir error'});
										}
									})
								}
								else{
									fs.rename(tmpPath,targetPath,function(err){
										if(err){
											res.send({'status':'error','message':'move file error'});
										}
										else{
											allPage[targetIndex].reply.push({
												userId:userId,
												type:type,
												title:title,
												content:content,
												time:pieTime(),
												_time:Math.floor(new Date().getTime()),
												subReply:[],
												file:{
													url:newSrc,
													name:req.files.file.name
												}
											});
											pdf.save(function(err){
												if(err){
													res.send({
														status:'error',
														message:JSON.stringify(err)
													})
												}
												else{
													res.send({
														status:'success'
													})
												}
											});
										}
									})
								}
							})
						}
						else{
							allPage[targetIndex].reply.push({
								userId:userId,
								type:type,
								title:title,
								content:content,
								time:pieTime(),
								_time:Math.floor(new Date().getTime()),
								subReply:[],
								file:null
							});
							pdf.save(function(err){
								if(err){
									res.send({
										status:'error',
										message:JSON.stringify(err)
									})
								}
								else{
									res.send({
										status:'success'
									})
								}
							});
						}
					}
				})
			}
		}
	})
}
exports.praiseToReply = function(req,res){
	var pdfUrl = req.body.pdfUrl;
	if(pdfUrl == null){
		res.send({
			status:'error',
			message:'pdfUrl不能为空'
		});
		return;
	}
	var pageIndex = parseInt(req.body.pageIndex);
	if(pageIndex<0){
		res.send({
			status:'error',
			message:'页码不能小于0'
		});
		return;
	}
	var replyId = req.body.replyId;
	if(!replyId){
		res.send({
			status:'error',
			message:'replyId不能为空'
		});
		return;
	}
	var userId = req.body.userId;
	if(!userId){
		res.send({
			status:'error',
			message:'userId不合法，对应用户不存在'
		});
		return;
	}
	var token = req.body.token;
	if(!token){
		res.send({
			status:'error',
			message:'token不能为空'
		});
		return;
	}
	pdfUserModel.findOne({'id':userId},function(err,user){
		if(err){
			res.send({
				status:'error',
				message:JSON.stringify(err)
			})
		}
		else{
			if(!user){
				res.send({
					status:'error',
					message:'userId不合法，对应用户不存在'
				})
			}
			else{
				if(user.token != token){
					res.send({
						status:'error',
						message:'token不合法'
					});
					return;
				}
				pdfModel.findOne({'url':pdfUrl},function(err,pdf){
					if(err){
						res.send({
							status:'error',
							message:JSON.stringify(err)
						})
					}
					else{
						if(!pdf){
							res.send({
								status:'error',
								message:'pdfUrl不存在'
							});
							return;
						}
						var allPage = pdf.page;
						var targetIndex = -1;
						var pageNum = allPage.length;
						for(var i = 0; i < pageNum; i++){
							if(allPage[i].index == pageIndex){
								targetIndex = i;
								break;
							}
						}
						if(targetIndex<0){
							res.send({
								status:'error',
								message:'pageIndex不合法，该页无评论'
							});
							return;
						}
						var allReply = allPage[targetIndex].reply;
						var replyNum = allReply.length;
						var replyIndex = -1;
						for(var i = 0; i < replyNum; i++){
							if(allReply[i]._id == replyId){
								replyIndex = i;
								break;
							}
						}
						if(replyIndex<0){
							res.send({
								status:'error',
								message:'replyId不合法，点赞的评论不存在'
							});
							return;
						}
						var idExist = allReply[replyIndex].praise.find(userId);
						if(idExist>=0){
							res.send({
								status:'error',
								message:'该用户已经赞过这条评论了'
							});
							return;
						}
						allReply[replyIndex].praise.push(userId);
						pdf.save(function(err){
							if(err){
								res.send({
									status:'error',
									message:JSON.stringify(err)
								})
							}
							else{
								res.send({
									status:'success'
								})
							}
						});
					}
				})
			}
		}
	})
}
exports.subReplyToReply = function(req,res){
	var pdfUrl = req.body.pdfUrl;
	if(pdfUrl == ''){
		res.send({
			status:'error',
			message:'pdfUrl不能为空'
		});
		return;
	}
	var pageIndex = parseInt(req.body.pageIndex);
	if(pageIndex<0){
		res.send({
			status:'error',
			message:'页码不能小于0'
		});
		return;
	}
	var replyId = req.body.replyId;
	if(!replyId){
		res.send({
			status:'error',
			message:'replyId不能为空'
		});
		return;
	}
	var content = req.body.content;
	if(content == ''){
		res.send({
			status:'error',
			message:'评论内容不能为空'
		});
		return;
	}
	var userId = req.body.userId;
	if(!userId){
		res.send({
			status:'error',
			message:'userId不合法，对应用户不存在'
		});
		return;
	}
	var token = req.body.token;
	if(!token){
		res.send({
			status:'error',
			message:'token不能为空'
		});
		return;
	}
	pdfUserModel.findOne({'id':userId},function(err,user){
		if(err){
			res.send({
				status:'error',
				message:JSON.stringify(err)
			})
		}
		else{
			if(!user){
				res.send({
					status:'error',
					message:'userId不合法，对应用户不存在'
				})
			}
			else{
				if(user.token != token){
					res.send({
						status:'error',
						message:'token不合法'
					});
					return;
				}
				pdfModel.findOne({'url':pdfUrl},function(err,pdf){
					if(err){
						res.send({
							status:'error',
							message:JSON.stringify(err)
						})
					}
					else{
						if(!pdf){
							res.send({
								status:'error',
								message:'pdfUrl不存在'
							});
							return;
						}
						var allPage = pdf.page;
						var targetIndex = -1;
						var pageNum = allPage.length;
						for(var i = 0; i < pageNum; i++){
							if(allPage[i].index == pageIndex){
								targetIndex = i;
								break;
							}
						}
						if(targetIndex<0){
							res.send({
								status:'error',
								message:'评论的页码不存在'
							})
						}
						else{
							var replyArray = allPage[targetIndex].reply;
							var rNum = replyArray.length;
							var replyIndex = -1;
							for(var i = 0; i < rNum; i++){
								if(replyArray[i]._id == replyId){
									replyIndex = i;
									break;
								}
							}
							if(replyIndex<0){
								res.send({
									status:'error',
									message:'replyId不合法'
								});
								return;
							}
							replyArray[replyIndex].subReply.push({
								'userId':userId,
								'content':content,
								'comment':[],
								'time':pieTime(),
								'_time':Math.floor(new Date().getTime())
							})
							pdf.save(function(err){
								if(err){
									res.send({
										status:'error',
										message:JSON.stringify(err)
									})
								}
								else{
									res.send({
										status:'success'
									})
								}
							});
						}
					}
				})
			}
		}
	})
}
exports.praiseToSubReply = function(req,res){
	var pdfUrl = req.body.pdfUrl;
	if(pdfUrl == null){
		res.send({
			status:'error',
			message:'pdfUrl不能为空'
		});
		return;
	}
	var pageIndex = parseInt(req.body.pageIndex);
	if(pageIndex<0){
		res.send({
			status:'error',
			message:'页码不能小于0'
		});
		return;
	}
	var replyId = req.body.replyId;
	if(!replyId){
		res.send({
			status:'error',
			message:'replyId不能为空'
		});
		return;
	}
	var subReplyId = req.body.subReplyId;
	if(!subReplyId){
		res.send({
			status:'error',
			message:'subReplyId不能为空'
		});
		return;
	}
	var userId = req.body.userId;
	if(!userId){
		res.send({
			status:'error',
			message:'userId不合法，对应用户不存在'
		});
		return;
	}
	var token = req.body.token;
	if(!token){
		res.send({
			status:'error',
			message:'token不能为空'
		});
		return;
	}
	pdfUserModel.findOne({'id':userId},function(err,user){
		if(err){
			res.send({
				status:'error',
				message:JSON.stringify(err)
			})
		}
		else{
			if(!user){
				res.send({
					status:'error',
					message:'userId不合法，对应用户不存在'
				})
			}
			else{
				if(user.token != token){
					res.send({
						status:'error',
						message:'token不合法'
					});
					return;
				}
				pdfModel.findOne({'url':pdfUrl},function(err,pdf){
					if(err){
						res.send({
							status:'error',
							message:JSON.stringify(err)
						})
					}
					else{
						if(!pdf){
							res.send({
								status:'error',
								message:'pdfUrl不存在'
							});
							return;
						}
						var allPage = pdf.page;
						var targetIndex = -1;
						var pageNum = allPage.length;
						for(var i = 0; i < pageNum; i++){
							if(allPage[i].index == pageIndex){
								targetIndex = i;
								break;
							}
						}
						if(targetIndex<0){
							res.send({
								status:'error',
								message:'pageIndex不合法，该页无评论'
							});
							return;
						}
						var allReply = allPage[targetIndex].reply;
						var rNum = allReply.length;
						var replyIndex = -1;
						for(var i = 0; i < rNum; i++){
							if(allReply[i]._id == replyId){
								replyIndex = i;
								break;
							}
						}
						if(replyIndex<0){
							res.send({
								status:'error',
								message:'replyId不合法，点赞的评论不存在'
							});
							return;
						}
						var allSubReply = allReply[replyIndex].subReply;
						var srNum = allSubReply.length;
						var subReplyIndex = -1;
						for(var i = 0; i < srNum; i++){
							if(allSubReply[i]._id == subReplyId){
								subReplyIndex = i;
								break;
							}
						}
						if(subReplyIndex<0){
							res.send({
								status:'error',
								message:'subReplyId不合法，点赞的评论不存在'
							});
							return;
						}
						var idExist = allSubReply[subReplyIndex].praise.find(userId);
						if(idExist>=0){
							res.send({
								status:'error',
								message:'该用户已经赞过这条评论了'
							});
							return;
						}
						allSubReply[subReplyIndex].praise.push(userId);
						pdf.save(function(err){
							if(err){
								res.send({
									status:'error',
									message:JSON.stringify(err)
								})
							}
							else{
								res.send({
									status:'success'
								})
							}
						});
					}
				})
			}
		}
	})
}
exports.commentToSubReply = function(req,res){
	var pdfUrl = req.body.pdfUrl;
	if(pdfUrl == ''){
		res.send({
			status:'error',
			message:'pdfUrl不能为空'
		});
		return;
	}
	var pageIndex = parseInt(req.body.pageIndex);
	if(pageIndex<0){
		res.send({
			status:'error',
			message:'页码不合法'
		});
		return;
	}
	var replyId = req.body.replyId;
	if(!replyId){
		res.send({
			status:'error',
			message:'replyId不能为空'
		});
		return;
	}
	var subReplyId = req.body.subReplyId;
	if(!subReplyIndex){
		res.send({
			status:'error',
			message:'subReplyId不能为空'
		});
		return;
	}
	var content = req.body.content;
	if(content == ''){
		res.send({
			status:'error',
			message:'评论内容不能为空'
		});
		return;
	}
	var target = parseInt(req.body.target);
	if(target < -1){
		res.send({
			status:'error',
			message:'target不合法'
		});
		return;
	}
	var userId = req.body.userId;
	if(!userId){
		res.send({
			status:'error',
			message:'userId不合法，对应用户不存在'
		});
		return;
	}
	var token = req.body.token;
	if(!token){
		res.send({
			status:'error',
			message:'token不能为空'
		});
		return;
	}
	pdfUserModel.findOne({'id':userId},function(err,user){
		if(err){
			res.send({
				status:'error',
				message:JSON.stringify(err)
			})
		}
		else{
			if(!user){
				res.send({
					status:'error',
					message:'userId不合法，对应用户不存在'
				})
			}
			else{
				if(user.token != token){
					res.send({
						status:'error',
						message:'token不合法'
					});
					return;
				}
				pdfModel.findOne({'url':pdfUrl},function(err,pdf){
					if(err){
						res.send({
							status:'error',
							message:JSON.stringify(err)
						});
					}
					else{
						if(!pdf){
							res.send({
								status:'error',
								message:'pdfUrl不存在'
							});
							return;
						}
						var allPage = pdf.page;
						var targetIndex = -1;
						var pageNum = allPage.length;
						for(var i = 0; i < pageNum; i++){
							if(allPage[i].index == pageIndex){
								targetIndex = i;
								break;
							}
						}
						if(targetIndex<0){
							res.send({
								status:'error',
								message:'评论的页码不存在'
							})
						}
						else{
							var replyArray = allPage[targetIndex].reply;
							var rNum = replyArray.length;
							var replyIndex = -1;
							for(var i = 0; i < rNum; i++){
								if(replyArray[i]._id == replyId){
									replyIndex = i;
									break;
								}
							}
							if(replyIndex<0){
								res.send({
									status:'error',
									message:'replyId不合法'
								});
								return;
							}
							var subReplyArray = replyArray[replyIndex].subReply;
							var srNum = subReplyArray.length;
							var subReplyIndex = -1;
							for(var i = 0; i < srNum; i++){
								if(subReplyArray[i]._id == subReplyId){
									subReplyIndex = i;
									break;
								}
							}
							if(subReplyIndex<0){
								res.send({
									status:'error',
									message:'subReplyId不合法'
								});
								return;
							}
							replyArray[replyIndex].subReply[subReplyIndex].comment.push({
								'userId':userId,
								'content':content,
								'target':target,
								'time':pieTime(),
								'_time':Math.floor(new Date().getTime())
							})
							pdf.save(function(err){
								if(err){
									res.send({
										status:'error',
										message:JSON.stringify(err)
									})
								}
								else{
									res.send({
										status:'success'
									})
								}
							});
						}
					}
				})
			}
		}
	})
}
exports.getReplyList = function(req,res){
	var urlQuery = url.parse(req.url).query;
	var pdfUrl = getStringKey(urlQuery,'pdfUrl');
	if(!pdfUrl){
		res.send({
			status:'error',
			message:'pdfUrl不能为空'
		});
		return;
	}
	var pageIndex = parseInt(getStringKey(urlQuery,'pageIndex'));
	if(pageIndex<0){
		res.send({
			status:'error',
			message:'页码不合法'
		});
		return;
	}
	pdfModel.findOne({'url':pdfUrl},function(err,pdf){
		if(err){
			res.send({
				status:'error',
				message:JSON.stringify(err)
			})
		}
		else{
			if(!pdf){
				res.send({
					status:'error',
					message:'url对应的pdf不存在'
				})
			}
			else{
				var pNum = pdf.page.length;
				var tPage = null;
				for(var i = 0; i < pNum; i++){
					if(pdf.page[i].index == pageIndex){
						tPage = pdf.page[i];
						break;
					}
				}
				if(tPage){
					res.send({
						status:'success',
						reply:tPage.reply
					});
				}
				else{
					res.send({
						status:'error',
						message:'当前页评论为空'
					});
				}
			}
		}
	})
}
exports.getReply = function(req,res){
	var urlQuery = url.parse(req.url).query;
	var pdfUrl = getStringKey(urlQuery,'pdfUrl');
	if(!pdfUrl){
		res.send({
			status:'error',
			message:'pdfUrl不能为空'
		});
		return;
	}
	var pageIndex = parseInt(getStringKey(urlQuery,'pageIndex'));
	if(pageIndex<0){
		res.send({
			status:'error',
			message:'pageIndex不合法'
		});
		return;
	}
	var replyId = getStringKey(urlQuery,'replyId');
	if(!replyId){
		res.send({
			status:'error',
			message:'replyId不能为空'
		});
		return;
	}
	pdfModel.findOne({'url':pdfUrl},function(err,pdf){
		if(err){
			res.send({
				status:'error',
				message:JSON.stringify(err)
			})
		}
		else{
			if(!pdf){
				res.send({
					status:'error',
					message:'url对应的pdf不存在'
				})
			}
			else{
				var pNum = pdf.page.length;
				var tPage = null;
				for(var i = 0; i < pNum; i++){
					if(pdf.page[i].index == pageIndex){
						tPage = pdf.page[i];
						break;
					}
				}
				if(tPage){
					var allReply = tPage.reply;
					var replyNum = allReply.length;
					var replyIndex = -1;
					for(var i = 0; i < replyNum; i++){
						if(allReply[i]._id == replyId){
							replyIndex = i;
							break;
						}
					}
					if(replyIndex<0){
						res.send({
							status:'error',
							message:'replyId不合法'
						});
					}
					else{
						res.send({
							status:'success',
							reply:allReply[replyIndex]
						});
					}
				}
				else{
					res.send({
						status:'error',
						message:'当前页评论为空'
					});
				}
			}
		}
	})
}
exports.getSubReply = function(req,res){
	var urlQuery = url.parse(req.url).query;
	var pdfUrl = getStringKey(urlQuery,'pdfUrl');
	if(!pdfUrl){
		res.send({
			status:'error',
			message:'pdfUrl不能为空'
		});
		return;
	}
	var pageIndex = parseInt(getStringKey(urlQuery,'pageIndex'));
	if(pageIndex<0){
		res.send({
			status:'error',
			message:'pageIndex不合法'
		});
		return;
	}
	var replyId = getStringKey(urlQuery,'replyId');
	if(!replyId){
		res.send({
			status:'error',
			message:'replyId不能为空'
		});
		return;
	}
	var subReplyId = getStringKey(urlQuery,'subReplyId');
	if(!subReplyId){
		res.send({
			status:'error',
			message:'subReplyId不能为空'
		});
		return;
	}
	pdfModel.findOne({'url':pdfUrl},function(err,pdf){
		if(err){
			res.send({
				status:'error',
				message:JSON.stringify(err)
			})
		}
		else{
			if(!pdf){
				res.send({
					status:'error',
					message:'url对应的pdf不存在'
				})
			}
			else{
				var pNum = pdf.page.length;
				var tPage = null;
				for(var i = 0; i < pNum; i++){
					if(pdf.page[i].index == pageIndex){
						tPage = pdf.page[i];
						break;
					}
				}
				if(tPage){
					var allReply = tPage.reply;
					var replyNum = allReply.length;
					var replyIndex = -1;
					for(var i = 0; i < replyNum; i++){
						if(allReply[i]._id == replyId){
							replyIndex = i;
							break;
						}
					}
					if(replyIndex<0){
						res.send({
							status:'error',
							message:'replyId不合法'
						});
					}
					else{
						var allSubReply = allReply[replyIndex].subReply;
						var subReplyNum = allSubReply.length;
						var subReplyIndex = -1;
						for(var i = 0; i < subReplyNum; i++){
							if(allSubReply[i]._id == subReplyId){
								subReplyIndex = i;
								break;
							}
						}
						if(subReplyIndex<0){
							res.send({
								status:'error',
								message:'subReplyId不合法'
							});
						}
						else{
							res.send({
								status:'success',
								reply:allSubReply[subReplyIndex]
							});
						}
					}
				}
				else{
					res.send({
						status:'error',
						message:'当前页评论为空'
					});
				}
			}
		}
	})
}